package com.sdp.th.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.redis.RedisUtil;
import com.sdp.core.bizc.vo.Result;
import com.sdp.th.db.th_eq_alarm_set.service.IThEqAlarmSetService;
import com.sdp.th.entity.ThEqAlarmSet;

/**
 * <p>
 * 设备报警设置 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2025-04-13
 */
@RestController
@RequestMapping("/ThEqAlarmSetController")
public class ThEqAlarmSetController {
	
	@Autowired
	private IThEqAlarmSetService iThEqAlarmSetService;
	
	@Autowired
	private RedisUtil redisUtil;


	@GetMapping(value="/queryAlarmSet")
	public Result<?> queryAlarmSet(@RequestParam(name="deviceNo",required=true) String deviceNo) {
		ThEqAlarmSet alarmSet = iThEqAlarmSetService.queryAlarmSetByDeviceNo(deviceNo);
		if(alarmSet==null) {
			alarmSet= new ThEqAlarmSet();
			alarmSet.setDeviceNo(deviceNo);
		}
		return Result.ok(alarmSet);
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody ThEqAlarmSet alarmSet) {
		
		if(alarmSet==null || StringUtils.isEmpty(alarmSet.getDeviceNo())) {
			return Result.error("报警设置信息为空");
		}
		if(StringUtils.isEmpty(alarmSet.getKeyId())) {
			iThEqAlarmSetService.save(alarmSet);
		}else {
			iThEqAlarmSetService.updateById(alarmSet);
		}
		
		redisUtil.setMap(ThEqAlarmSet.class.getName(), alarmSet.getDeviceNo(), alarmSet);
		return Result.ok(alarmSet);
	}
}

